
class Solution:
    def validIPAddress(self, queryIP: str) -> str:
        if self.isIPv4(queryIP):
            return "IPv4"
        elif self.isIPv6(queryIP):
            return "IPv6"
        else:
            return "Neither"

    def isIPv4(self, queryIP):
        if queryIP.count('.') != 3:
            return False
        for i in queryIP.split('.'):
            if not i.isdigit() or int(i) > 255 or int(i) < 0:
                return False
            if str(int(i)) != i:
                return False
        return True
    
    def isIPv6(self, queryIP):
        if queryIP.count(':') != 7:
            return False
        for i in queryIP.split(':'):
            try:
                num = int('0x' + i, 16)
                if num > 65535 or num < 0 or len(i) > 4:
                    return False
            except:
                return False
        return True